package com.project.les;

import com.easy.facebook.android.apicall.GraphApi;
import com.easy.facebook.android.data.User;
import com.easy.facebook.android.error.EasyFacebookError;
import com.easy.facebook.android.facebook.FBLoginManager;
import com.easy.facebook.android.facebook.Facebook;
import com.easy.facebook.android.facebook.LoginListener;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class FacebookPost {
	/** Called when the activity is first created. */

	private FBLoginManager fbLoginManager;

	// Facebook App ID:
	public final String APP_ID = "198440656904529";
	
	private static FacebookPost __instance;
	private Activity callerActivity;

	private FacebookPost() {}
	
	public void setCallerActivity(Activity activity) {
		this.callerActivity = activity;
	}
	
	public Activity getCallerActivity() throws Exception {
		if (this.callerActivity == null)
			throw new Exception("Activity não definida");
		
		return this.callerActivity;
	}
	
	public static FacebookPost getInstance() {
		if (FacebookPost.__instance == null)
			FacebookPost.__instance = new FacebookPost();
		
		return FacebookPost.__instance;
	}
	
	public void setFacebookData(Intent data) {
		this.fbLoginManager.loginSuccess(data);
	}

	public void login() {

		// read about Facebook Permissions here:
		// http://developers.facebook.com/docs/reference/api/permissions/
		/*String permissions[] = { "user_about_me", "user_activities",
				"user_birthday", "user_checkins", "user_education_history",
				"user_events", "user_groups", "user_hometown",
				"user_interests", "user_likes", "user_location", "user_notes",
				"user_online_presence", "user_photo_video_tags", "user_photos",
				"user_relationships", "user_relationship_details",
				"user_religion_politics", "user_status", "user_videos",
				"user_website", "user_work_history", "email",

				"read_friendlists", "read_insights", "read_mailbox",
				"read_requests", "read_stream", "xmpp_login", "ads_management",
				"create_event", "manage_friendlists", "manage_notifications",
				"offline_access", "publish_checkins", "publish_stream",
				"rsvp_event", "sms",
				"publish_actions",

				"manage_pages"

		};
		/**/
		
		String permissions[] = { "publish_stream" };
		
		try {
			fbLoginManager = new FBLoginManager(this.getCallerActivity(), R.layout.view_feriado,
					APP_ID, permissions);
	
			if (fbLoginManager.existsSavedFacebook()) {
				fbLoginManager.loadFacebook();
			} else {
				fbLoginManager.login();
			}
		} catch (Exception e) {
			Log.e("ERROR", e.getMessage());
			//e.printStackTrace();
			//showToast(e.getMessage());
			this.showToast("Não foi possível conectar-se ao Facebook. Verifique sua conexão com a internet.");
		}
	}
	
	private void showToast(String s) {
		try {
			Toast.makeText(this.getCallerActivity(), s, Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void facebookMessage(Facebook facebook, String message) {
		
		try {
			
			GraphApi graphApi = new GraphApi(facebook);

			// Para adquirir mais informações do usuário. É necessário setar as permissões.
			//User user = new User();
			//user = graphApi.getMyAccountInfo();
			
			// Postando uma mensagem no Facebook
			graphApi.setStatus(message);
			
			this.showToast("Feriado postado no Facebook com sucesso!");
			
			// Para deslogar o usuário:
			//fbLoginManager.logout(facebook);
			
			Log.w("WARNING", "Feriado postado no Facebook com sucesso!");
			
		} catch (EasyFacebookError e) {
			if (e.getMessage().contains("Duplicate status message"))
				this.showToast("Você já postou este feriado no Facebook!");
			else
				this.showToast("Faça o login antes de postar no Facebook.");
			Log.d("ERROR: ", e.getMessage());
			
		} catch (Exception e) {
			e.printStackTrace();
			this.showToast("Ocorreu um erro ao postar no Facebook, tente mais tarde.");
			//showToast(e.getMessage());
		}
	}

	public void loginSuccess(Facebook facebook, String message) {
		this.facebookMessage(facebook, message);
	}

	public void logoutSuccess() {
		this.showToast("Deslogado com sucesso!");
	}

	public void loginFail() {
		this.showToast("Não foi possível conectar-se ao Facebook. Verifique sua conexão com a internet.");
	}
}